package edu.chl.food_store.entities;

import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * Customer Entity:
 * Uses the Address as an embeddable entity.
 * Have some named queries attached to it...
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "findAllCustomers",
    query = "SELECT c FROM Customer c"),
    @NamedQuery(name = "findCustomerByEmail",
    query = "SELECT c FROM Customer c WHERE c.email = :custEmail")
})
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotNull
    String fname;
    @NotNull
    String lname;
    @Column(name = "EMAIL", nullable = false, unique = true)
    String email;
    @Column(name = "PASSWORD", nullable = false)
    String pass;
    @Embedded
    Address address = new Address();
    @NotNull
    boolean administrator;

    public Customer() {
    }

    public Customer(String fname, String lname, String email, String pass, Address address) {
        this.fname = fname;
        this.lname = lname;
        this.email = email;
        this.pass = pass;
        this.address = address;
        //New customers never get admin-status by default. Change this value
        //manually in database to give customer admin-status.
        this.administrator = false;
    }

    public boolean isAdministrator() {
        return administrator;
    }

    public void setAdministrator(boolean administrator) {
        this.administrator = administrator;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 50 + hash + (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.food_store.entities.Customer[id=" + id + "]";
    }
}
